package com.googlecode.flealancemarket.tapestry.pages;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.Job;
import com.googlecode.flealancemarket.metamodel.JobMessage;
import com.googlecode.flealancemarket.service.commands.GetJobWithMessages;
import com.googlecode.flealancemarket.service.commands.SaveJobMessage;
import com.googlecode.flealancemarket.tapestry.util.Session;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.ioc.annotations.Inject;

public class JobDetail {

    private Job job;

    private int desiredAmount;
    private int expectedTime;
    private String text;
    private long jobId;

    private JobMessage jobMessage;
    private List<JobMessage> jobMessages;

    public Job getJob(){
        return job;
    }
    public void setJob(Job job){
        this.job = job;
    }

    public int getDesiredAmount() {
        return desiredAmount;
    }

    public void setDesiredAmount(int desiredAmount) {
        this.desiredAmount = desiredAmount;
    }

    public int getExpectedTime() {
        return expectedTime;
    }

    public void setExpectedTime(int expectedTime) {
        this.expectedTime = expectedTime;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public long getJobId() {
        return jobId;
    }
    public void setJobId(long jobId) {
        this.jobId = jobId;
    }
    public JobMessage getJobMessage() {
        return jobMessage;
    }
    public void setJobMessage(JobMessage jobMessage) {
        this.jobMessage = jobMessage;
    }
    public List<JobMessage> getJobMessages(){
        return jobMessages;
    }
    public void setJobMessages(List<JobMessage> jobMessages){
        this.jobMessages = jobMessages;
    }
    public boolean getUserIsLogged(){
        return (session.getUserId()!=0 && !session.getUserIsEmployer());
    }
    public boolean getJobHasMessages(){
        return !jobMessages.isEmpty();
    }
    public boolean getIsActive(){
        return Calendar.getInstance().getTimeInMillis() < job.getEndingTime().getTime();
    }

    @ApplicationState
    private Session session;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.GetJobWithMessages")
    private GetJobWithMessages getJob;
    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.SaveJobMessage")
    private SaveJobMessage saveJobMessage;
    @Inject
    @SpringBean("com.googlecode.flealancemarket.metamodel.JobMessage")
    private JobMessage postedMessage;

    Object onActivate(int jobId){
        this.jobId = jobId;
        getJob.setJobId((long)jobId);
        job = getJob.execute();
        if(job==null){
            session.setMessage("That job doesn't exist.");
            return Categories.class;
        }
        jobMessages = job.getJobMessages();
        return null;
    }

    long onPassivate(){
        return this.jobId;
    }

    Object onSubmitFromJobMessageForm(){
        postedMessage.setAmount(desiredAmount);
        postedMessage.setEstimatedTime(expectedTime);
        postedMessage.setText(text);
        postedMessage.setJob(job);
        saveJobMessage.setJobMessage(postedMessage);
        saveJobMessage.setFreelancerId(session.getUserId());
        JobMessage jm = saveJobMessage.execute();
        if(jm==null)
            session.setMessage("Somethign went wrong. Please, send your message again.");
        else
            session.setMessage("Message successfully posted.");
        return this;
    }
}
